package com.easyOrder.service.impl.push;

/**
 * Created by liutao on 2017/7/21.
 */

import com.easyOrder.core.push.PushMessage;
import com.easyOrder.core.push.PushResult;
import org.apache.log4j.Logger;

import java.text.MessageFormat;
import java.util.concurrent.RecursiveTask;

public class MessagePushTask extends RecursiveTask<PushResult> {

    private PushMessage message;

    private IMessagePushChannel channels;

    private boolean pushMessages;

    private static final long serialVersionUID = -8036531638968473497L;
    private static final Logger logger = Logger.getLogger(MessagePushTask.class);

    public MessagePushTask(PushMessage message, IMessagePushChannel pushmessgeExecute, boolean pushMessages) {
        super();
        this.message = message;
        this.channels = pushmessgeExecute;
        this.pushMessages = pushMessages;
    }

    /**
     * 消息推送
     * @return
     */
    @Override
    protected PushResult compute() {
        if (!pushMessages) {
            logger.warn("push message paramete is false, skip push:" + pushMessages + "|" + channels.getChannelName());
            logger.warn("message:" + message.toString());
            return null;
        }
        try {
            PushResult result = channels.push(message);
            logger.warn("debuginfo:" + pushMessages + "|" + channels.getChannelName());
            logger.warn("message:" + message.toString());
            if (logger.isDebugEnabled()) {
                logger.debug(MessageFormat.format(
                        "{0} push message {1} , got response result : {2}", channels.getChannelName(), message,
                        result));
            }
            return result;
        } catch (Exception e) {
            logger.error(e, e);
        }
        return null;
    }

}

